package com.xjgzinfo.usdp.service;

import com.xjgzinfo.usdp.domain.BladeVisualComponent;
import com.xjgzinfo.usdp.domain.BladeVisualComponent_;
import com.xjgzinfo.usdp.jpa.service.QueryService;
import com.xjgzinfo.usdp.repository.BladeVisualComponentRepository;
import com.xjgzinfo.usdp.service.dto.BladeVisualComponentCriteria;
import com.xjgzinfo.usdp.service.dto.BladeVisualComponentDTO;
import com.xjgzinfo.usdp.service.mapper.BladeVisualComponentMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Service for executing complex queries for {@link BladeVisualComponent} entities in the database.
 * The main input is a {@link BladeVisualComponentCriteria} which gets converted to {@link Specification},
 * in a way that all the filters must apply.
 * It returns a {@link List} of {@link BladeVisualComponentDTO} or a {@link Page} of {@link BladeVisualComponentDTO} which fulfills the criteria.
 */
@Service
@Transactional(readOnly = true)
public class BladeVisualComponentQueryService extends QueryService<BladeVisualComponent> {

    private final Logger log = LoggerFactory.getLogger(BladeVisualComponentQueryService.class);

    private final BladeVisualComponentRepository bladeVisualComponentRepository;

    private final BladeVisualComponentMapper bladeVisualComponentMapper;

    public BladeVisualComponentQueryService(BladeVisualComponentRepository bladeVisualComponentRepository, BladeVisualComponentMapper bladeVisualComponentMapper) {
        this.bladeVisualComponentRepository = bladeVisualComponentRepository;
        this.bladeVisualComponentMapper = bladeVisualComponentMapper;
    }

    /**
     * Return a {@link List} of {@link BladeVisualComponentDTO} which matches the criteria from the database.
     *
     * @param criteria The object which holds all the filters, which the entities should match.
     * @return the matching entities.
     */
    @Transactional(readOnly = true)
    public List<BladeVisualComponentDTO> findByCriteria(BladeVisualComponentCriteria criteria) {
        log.debug("find by criteria : {}", criteria);
        final Specification<BladeVisualComponent> specification = createSpecification(criteria);
        return bladeVisualComponentMapper.toDto(bladeVisualComponentRepository.findAll(specification));
    }

    /**
     * Return a {@link Page} of {@link BladeVisualComponentDTO} which matches the criteria from the database.
     *
     * @param criteria The object which holds all the filters, which the entities should match.
     * @param page     The page, which should be returned.
     * @return the matching entities.
     */
    @Transactional(readOnly = true)
    public Page<BladeVisualComponentDTO> findByCriteria(BladeVisualComponentCriteria criteria, Pageable page) {
        log.debug("find by criteria : {}, page: {}", criteria, page);
        final Specification<BladeVisualComponent> specification = createSpecification(criteria);
        return bladeVisualComponentRepository.findAll(specification, page)
                .map(bladeVisualComponentMapper::toDto);
    }

    /**
     * Return the number of matching entities in the database.
     *
     * @param criteria The object which holds all the filters, which the entities should match.
     * @return the number of matching entities.
     */
    @Transactional(readOnly = true)
    public long countByCriteria(BladeVisualComponentCriteria criteria) {
        log.debug("count by criteria : {}", criteria);
        final Specification<BladeVisualComponent> specification = createSpecification(criteria);
        return bladeVisualComponentRepository.count(specification);
    }

    /**
     * Function to convert {@link BladeVisualComponentCriteria} to a {@link Specification}
     *
     * @param criteria The object which holds all the filters, which the entities should match.
     * @return the matching {@link Specification} of the entity.
     */
    protected Specification<BladeVisualComponent> createSpecification(BladeVisualComponentCriteria criteria) {
        Specification<BladeVisualComponent> specification = Specification.where(null);
        if (criteria != null) {
            if (criteria.getId() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getId(), BladeVisualComponent_.id));
            }
            if (criteria.getName() != null) {
                specification = specification.and(buildStringSpecification(criteria.getName(), BladeVisualComponent_.name));
            }
        }
        return specification;
    }
}